//
//  FieldViewController.m
//  Final
//
//  Created by student14 on 2021/12/17.
//  Copyright © 2021 Lenrony. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "../ModelController.h"
#import "FieldViewController.h"
#import "DetailViewController.h"
#import "PostViewController.h"
#import "./Views/ArticleView.h"

float screenWidth = 414.0;
float screenHeight = 896.0;

@interface FieldViewController()

@property (strong, nonatomic) NSMutableArray *buttonList;
@property int selectedField;
@property (strong, nonatomic) UIColor *selectedColor;
@property (strong, nonatomic) UIButton *postButton;
@property (strong, nonatomic) UIButton *returnButton;

@property (strong, nonatomic) UIAlertController *alert;
@property (strong, nonatomic) NSMutableArray *rawUsers;
@property (strong ,nonatomic) NSMutableArray *articles;
@property (strong, nonatomic) ArticleView *artv;

@property(strong, nonatomic) ModelController *mc;

@end

@implementation FieldViewController

- (FieldViewController *)initWithField: (int)field {
    self = [super init];
    _selectedField = field;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _mc = [ModelController getInstance];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"gray.png"]];
    [self setupNavigationView];
    [self setupButtons];
    [self setupCollectionView];
    [self.view insertSubview:_postButton aboveSubview:_artv];
    [self.view insertSubview:_returnButton aboveSubview:_artv];
}

- (void)setupNavigationView {
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 90)];
    navView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:navView];
    
    _buttonList = [[NSMutableArray alloc] init];
    _selectedColor = [UIColor colorWithRed:50/255.0 green:153/255.0 blue:50/255.0 alpha:1.0];
    
    for (int i = 0; i < 6; i++) {
        UIButton *ibtr = [[UIButton alloc] initWithFrame:CGRectMake(20+i*64, 50, 54, 30)];
        switch (i) {
            case 0:
                [ibtr setTitle:@"全部帖子" forState:UIControlStateNormal];
                break;
            case 1:
                [ibtr setTitle:@"二手闲置" forState:UIControlStateNormal];
                break;
            case 2:
                [ibtr setTitle:@"打听求助" forState:UIControlStateNormal];
                break;
            case 3:
                [ibtr setTitle:@"青青子衿" forState:UIControlStateNormal];
                break;
            case 4:
                [ibtr setTitle:@"瓜田趣事" forState:UIControlStateNormal];
                break;
            case 5:
                [ibtr setTitle:@"校园招聘" forState:UIControlStateNormal];
                break;
        }
        if (i == _selectedField) {
            [ibtr setBackgroundColor:_selectedColor];
            [ibtr setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
        else {
            [ibtr setBackgroundColor:[UIColor clearColor]];
            [ibtr setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        ibtr.titleLabel.font = [UIFont systemFontOfSize:11];
        ibtr.layer.cornerRadius = 5;
        ibtr.layer.masksToBounds = YES;
        [ibtr addTarget:self action:@selector(navigationClick:) forControlEvents:UIControlEventTouchUpInside];
        [_buttonList addObject:ibtr];
        [navView addSubview:ibtr];
    }
}

- (void)setupButtons {
    _postButton = [[UIButton alloc] initWithFrame:CGRectMake(screenWidth-50, screenHeight-195, 40, 40)];
    [_postButton setImage:[UIImage imageNamed:@"text_edit.png" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
    [_postButton setBackgroundColor:[UIColor clearColor]];
    [_postButton setTintColor:[UIColor greenColor]];
//    _postButton.layer.borderWidth = 1;
//    [_postButton.layer setBorderColor:_selectedColor.CGColor];
    [_postButton addTarget:self action:@selector(addNewPostClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_postButton];
    
    _returnButton = [[UIButton alloc] initWithFrame:CGRectMake(screenWidth-50, screenHeight-150, 40, 40)];
    [_returnButton setImage:[UIImage imageNamed:@"home.png" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
    [_returnButton setBackgroundColor:[UIColor clearColor]];
    [_returnButton setTintColor:[UIColor greenColor]];
    _returnButton.layer.borderWidth = 1;
    [_returnButton.layer setBorderColor:_selectedColor.CGColor];
    [_returnButton addTarget:self action:@selector(returnButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_returnButton];
}

- (void)setupCollectionView {
    self.articles = [[ModelController getInstance] getArticleWithType:_selectedField andMode:@"time"];
    if (self.articles == nil) {
        [self showAlertWithTitle:@"联网失败" andText:@"请确认网络连接正常并刷新！"];
        self.articles = [[NSMutableArray alloc] init];
    }
    if (self.articles.count == 0) {
        [self showAlertWithTitle:@"当前没有帖子" andText:@"未找到相关帖子..."];
        self.articles = [[NSMutableArray alloc] init];
    }
    
    self.rawUsers = [[NSMutableArray alloc] init];
    for (Article *art in self.articles) {
        [self.rawUsers addObject:[[ModelController getInstance] getRawUser:art.u_id]];
    }
    
    self.artv = [[ArticleView alloc] initWithViewController:self andRawUsers:self.rawUsers andArticles:self.articles];
    [self.artv setFrame:CGRectMake(0, 90, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height-90-[[UIApplication sharedApplication] statusBarFrame].size.height)];
    [self.artv.articlesView setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height-90-[[UIApplication sharedApplication] statusBarFrame].size.height)];
    [self.view addSubview:self.artv];
}

- (void)updateCollectionView {
    self.articles = [[ModelController getInstance] getArticleWithType:_selectedField andMode:@"time"];
    if (self.articles == nil) {
        [self showAlertWithTitle:@"联网失败" andText:@"请确认网络连接正常并刷新！"];
        self.articles = [[NSMutableArray alloc] init];
    }
    if (self.articles.count == 0) {
        [self showAlertWithTitle:@"当前没有帖子" andText:@"未找到相关帖子..."];
        self.articles = [[NSMutableArray alloc] init];
    }
    
    self.rawUsers = [[NSMutableArray alloc] init];
    for (Article *art in self.articles) {
        [self.rawUsers addObject:[[ModelController getInstance] getRawUser:art.u_id]];
    }
    
    [self.artv updateCollectionViewWithArticles:self.articles andRawUsers:self.rawUsers];
}

- (void)navigationClick:(id)sender {
    for (int i = 0; i < _buttonList.count; i++) {
        if (sender == _buttonList[i]) {
            [_buttonList[i] setBackgroundColor:_selectedColor];
            [_buttonList[i] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            _selectedField = i;
        }
        else {
            [_buttonList[i] setBackgroundColor:[UIColor clearColor]];
            [_buttonList[i] setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    [self updateCollectionView];
}

- (void)addNewPostClick:(id)sender {
    if (_mc.cur_user == nil) {
        [self showAlertWithTitle:@"创建帖子失败" andText:@"请先登录！"];
    }
    else {
        PostViewController *pvc = [[PostViewController alloc] init];
        [self.navigationController pushViewController:pvc animated:YES];
    }
}

- (void)returnButtonClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)showAlertWithTitle: (NSString*)title
                   andText: (NSString*)text
{
    self.alert = [UIAlertController alertControllerWithTitle:title message:text preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:self.alert animated:YES completion:nil];
    [self performSelector:@selector(hideAlert) withObject:self.alert afterDelay:1.5];
}

- (void)hideAlert
{
    [self.alert dismissViewControllerAnimated:YES completion:^{
        if(self.mc.cur_user != nil){
            [self viewWillAppear:YES];
        }
    }];
}

@end
